package com.whl.basic.service.impl;

import com.whl.basic.mapper.BaseMapper;
import com.whl.basic.query.BaseQuery;
import com.whl.basic.service.IBaseService;
import com.whl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公共业务实现类
 */

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
        
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        // 查询总条数
        Long count = baseMapper.queryCount(query);
        if (count == 0){
            return new PageList<T>();
        }

        // 查询当前页的分页数据
        List<T> data = baseMapper.queryData(query);

        return new PageList<T>(count,data);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }
}
